<?php
// +----------------------------------------------------------------------
// | Fanwe 方维订餐小秘书商业系统
// +----------------------------------------------------------------------
// | Copyright (c) 2011 http://www.fanwe.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: 云淡风轻(88522820@qq.com)
// +----------------------------------------------------------------------

require APP_ROOT_PATH.'app/Lib/deal.php';
require APP_ROOT_PATH.'app/Lib/store_lib.php';
class indexModule extends ShopBaseModule
{
	public function index()
	{	
		$GLOBALS['tmpl']->caching = true;
		$GLOBALS['tmpl']->cache_lifetime = 600;  //首页缓存10分钟
		$cache_id  = md5(MODULE_NAME.ACTION_NAME.$GLOBALS['deal_city']['id']);	
		$template =  $GLOBALS['deal_city']['is_open'] ==1 ? "index.html" : "index_wait.html";	
		if (!$GLOBALS['tmpl']->is_cached($template, $cache_id))
		{	
			make_deal_cate_js();
			make_deal_region_js();	
			$city_id = intval($GLOBALS['deal_city']['id']);
			if($city_id>0)
			{			
				$city_name = $GLOBALS['db']->getOne("select name from ".DB_PREFIX."deal_city where is_effect = 1 and is_delete = 0 and id = ".intval($city_id));
				$GLOBALS['tmpl']->assign("city_name",$city_name);
				$city_ids = load_auto_cache("deal_city_belone_ids",array("city_id"=>$city_id));
			}
			$result = load_auto_cache("store_filter_nav_cache",array('city_id'=>$city_id));
			
			$GLOBALS['tmpl']->assign("cate_list",$result['cate_list']);
			$GLOBALS['tmpl']->assign("area_list",$result['area_list']);
			$GLOBALS['tmpl']->assign("purpose_list",$result['purpose_list']);
			$GLOBALS['tmpl']->assign("avg_price_list",$result['avg_price_list']);
			
			//推荐目的
			$res_purpose_list = $GLOBALS['db']->getAll("SELECT pc.* FROM ".DB_PREFIX."purpose_cate pc LEFT JOIN ".DB_PREFIX."purpose_cate_city_link pccl ON pccl.cate_id = pc.id WHERE pccl.recommend=1 AND pccl.city_id=$city_id AND pc.is_delete=0 GROUP BY pc.id ORDER BY pccl.rec_sort DESC");
			foreach($res_purpose_list as $k=>$v){
				//小类数量	
				$res_purpose_list[$k]['cate_count'] = $GLOBALS['db']->getOne("select count(distinct pct.id) from ".DB_PREFIX."purpose_cate_type pct LEFT JOIN ".DB_PREFIX."purpose_cate_type_city_link pctcl ON pctcl.cate_type_id=pct.id WHERE pctcl.city_id in(".implode(",",$city_ids).") AND pct.cate_id=".$v['id']);
				//推荐到首页的小类目的
				$res_purpose_list[$k]['sub'] = $GLOBALS['db']->getAll("SELECT pct.* FROM ".DB_PREFIX."purpose_cate_type pct LEFT JOIN ".DB_PREFIX."purpose_cate_type_city_link pctcl ON pctcl.cate_type_id = pct.id WHERE pctcl.recommend=1 AND pctcl.city_id in(".implode(",",$city_ids).") AND pct.cate_id=".$v['id']." GROUP BY pct.id ORDER BY pctcl.rec_sort DESC LIMIT 5");
				foreach($res_purpose_list[$k]['sub'] as $kk=>$vv){
					$condition= "";
					$condition = build_deal_filter_condition(array("pid"=>$v['id'],"sid"=>$vv['id']));		
					$res_purpose_list[$k]['sub'][$kk]['count'] = $GLOBALS['db']->getOne("select count(*) from ".DB_PREFIX."supplier_location where deal_type=0 AND is_effect=1 $condition ");
					//推荐的餐厅
					if($kk==0){
						$res_purpose_list[$k]['sub'][$kk]['rec_res'] = $GLOBALS['db']->getRow("select * from ".DB_PREFIX."supplier_location where deal_type=0 AND is_effect=1 $condition ORDER BY is_recommend DESC,sort DESC,avg_point DESC,id ASC");
					}
					$res_purpose_list[$k]['sub'][$kk]['url'] = url("store","index",array("pid"=>$v['id'],"sid"=>$vv['id']));
				}
				$res_purpose_list[$k]['url'] = url("store","index",array("pid"=>$v['id']));
			}
			
			$GLOBALS['tmpl']->assign("res_purpose_list",$res_purpose_list);
			
			if($city_id>0 && $city_ids!=''){
				$index_city=" and city_id in (".implode(",",$city_ids).")";
			}	
			//热门现金券		
			$hot_vouche=$GLOBALS['db']->getAll("select id,icon,current_price,origin_price,supplier_id,name from ".DB_PREFIX."deal where is_shop=2 $index_city  and is_delete=0 and is_effect=1 and is_hot=1 order by sort desc limit 3");
			foreach($hot_vouche as $k=>$v)
			{
				$hot_vouche[$k]['supplier_name']=$GLOBALS['db']->getOne("select name from ".DB_PREFIX."supplier where id=".$v['supplier_id']);
				$hot_vouche[$k]['format_origin_price']=format_price($v['origin_price']);
				$hot_vouche[$k]['format_current_price']=format_price($v['current_price']);	
				$hot_vouche[$k]['discount'] = round(($v['current_price']/$v['origin_price'])*10,2);
				$hot_vouche[$k]['url'] =url("store","ydetail",array("id"=>$v['id']));
			}
			$GLOBALS['tmpl']->assign("hot_vouche",$hot_vouche);
			
			//热门餐厅
			$hot_where = " deal_type=0 and is_effect=1 and is_hot=1";
			$hot_sortby = " is_recommend desc,sort desc,is_verify desc,dp_count desc ";
			$store_list = get_store_list(5,0,$hot_where,$hot_ortby,false);
			$hot_store_list=$store_list['list'];
			foreach($hot_store_list as $k=>$v)
			{
				$hot_store_list[$k]['avg_point_100']=round($v['avg_point']/5.0,2)*100;
			}				
			$GLOBALS['tmpl']->assign("hot_store_list",$hot_store_list);
			
			//商家个数
			$c_supplier_count=$GLOBALS['db']->getOne("select count(*) from ".DB_PREFIX."supplier_location where is_effect=1 $index_city");
			$GLOBALS['tmpl']->assign("c_supplier_count",$c_supplier_count);
			//我是商户的链接
			$s_account_info = es_session::get("account_info");
			if(intval($s_account_info['id'])==0)
			{
				$s_url=url("biz","join");
			}
			else
			{
				$account_data = $GLOBALS['db']->getRow("select s.name as name,a.account_name as account_name,a.login_ip as login_ip ,a.login_time as login_time ,a.update_time as create_time, a.supplier_id as supplier_id from ".DB_PREFIX."supplier_account as a left join ".DB_PREFIX."supplier as s on a.supplier_id = s.id where a.id = ".intval($s_account_info['id']));
				if($account_data)
				{
					$s_url=url("biz","index");
				}
				$GLOBALS['tmpl']->assign("account_data",$account_data);	
			}
			$GLOBALS['tmpl']->assign("s_url",$s_url);
			//今日热点
			$tcate_id=$GLOBALS['db']->getOne("select id from ".DB_PREFIX."article_cate where title='今日热点'");
			if($tcate_id){
				$hot_topic=$GLOBALS['db']->getAll("select id,title,uname from ".DB_PREFIX."article where cate_id=".$tcate_id." order by sort desc limit 3");
				foreach($hot_topic as $k=>$v)
				{
					if($v['uname']!='')
					$turl = url("shop","notice",array("id"=>$v['uname']));
					else
					$turl = url("shop","notice",array("id"=>$v['id']));
					$hot_topic[$k]['url']=$turl;
				}
				$GLOBALS['tmpl']->assign("hot_topic",$hot_topic);
			}
			//活动公告
			$acate_id=$GLOBALS['db']->getOne("select id from ".DB_PREFIX."article_cate where title='活动公告'");
			if($acate_id){
				$hot_activity=$GLOBALS['db']->getAll("select id,title,uname from ".DB_PREFIX."article where cate_id=".$acate_id." order by sort desc limit 3");
				foreach($hot_activity as $k=>$v)
				{
					if($v['uname']!='')
					$aurl = url("shop","notice",array("id"=>$v['uname']));
					else
					$aurl = url("shop","notice",array("id"=>$v['id']));
					$hot_activity[$k]['url']=$aurl;
				}
				$hot_activity=$GLOBALS['tmpl']->assign("hot_activity",$hot_activity);
			}
			
			//团购推荐
			$index_tuan_cate=$GLOBALS['db']->getAll("select dc.* from ".DB_PREFIX."deal_cate dc LEFT JOIN ".DB_PREFIX."deal_cate_city_link dccl ON dccl.cate_id=dc.id where dccl.city_id=$city_id AND dc.is_effect = 1 and dc.is_delete = 0 and dc.pid = 0 and dc.recommend=1 order by dc.sort desc limit 4");
			$index_tuan_list=get_deal_list(4,$cate_id=0,$city_id, $type=array(DEAL_ONLINE,DEAL_HISTORY,DEAL_NOTICE), $where="is_recommend=1",$orderby = '' , $quan_id=0);
			$GLOBALS['tmpl']->assign("index_tuan_cate",$index_tuan_cate);
			$GLOBALS['tmpl']->assign("index_tuan_list",$index_tuan_list['list']);
			//print_r($index_tuan_list);
			
			//优惠券推荐
			 $free_youhui_list = get_free_youhui_list(5,$cate_id=0," is_recommend=1","");
			 $free_youhui_list=$free_youhui_list['list'];
			 foreach($free_youhui_list as $k=>$v)
			 {
			 	$free_youhui_list[$k]['supplier_name']=$GLOBALS['db']->getOne("select name from ".DB_PREFIX."supplier where id=".$v['supplier_id']);
			 }
			 $GLOBALS['tmpl']->assign("free_youhui_list",$free_youhui_list);
			 //print_r($free_youhui_list);
			 
			 //活动推荐
			 $event_cate_list=$GLOBALS['db']->getAll("select * from ".DB_PREFIX."event_cate where is_effect=1 order by sort desc limit 4");
			 $event_list=search_event_list(5, $cate_id=0, $city_id, $where=' is_recommend=1',$orderby = '',$cached = false);
			 $GLOBALS['tmpl']->assign("event_cate_list",$event_cate_list);
			 $GLOBALS['tmpl']->assign("event_list",$event_list['list']);
			 //print_r($event_list);
			
			//开始输出友情链接
			$f_link_group = $GLOBALS['db']->getAll("select * from ".DB_PREFIX."link_group where is_effect = 1 order by sort desc");
			foreach($f_link_group as $k=>$v)
			{
				$g_links = $GLOBALS['db']->getAll("select * from ".DB_PREFIX."link where is_effect = 1 and show_index = 1 and group_id = ".$v['id']." order by sort desc");
				if($g_links)
				{
					foreach($g_links as $kk=>$vv)
					{
						if(substr($vv['url'],0,7)=='http://')
						{
							$g_links[$kk]['url'] = str_replace("http://","",$vv['url']);
						}
					}
					$f_link_group[$k]['links'] = $g_links;
				}
				else
				unset($f_link_group[$k]);
			}
			
			
			$now = get_gmtime();
			$vote = $GLOBALS['db']->getRow("select * from ".DB_PREFIX."vote where is_effect = 1 and begin_time < ".$now." and (end_time = 0 or end_time > ".$now.") order by sort desc limit 1");
			$GLOBALS['tmpl']->assign("vote",$vote);
			$GLOBALS['tmpl']->assign("f_link_data",$f_link_group);
			
			$res_sql= "";
			
			if($GLOBALS['deal_city']['is_open'] ==0){ 
				if($city_ids)
				{
					$res_sql = " and city_id in (".implode(",",$city_ids).")";
				}
				$res_count =  $GLOBALS['db']->getOne("select count(*) from ".DB_PREFIX."supplier_location use index(search_idx5) where deal_type=0 AND is_effect = 1 $res_sql ");			
				$GLOBALS['tmpl']->assign("res_count",$res_count);
				$res_list = $GLOBALS['db']->getAll("SELECT * FROM ".DB_PREFIX."supplier_location WHERE deal_type=0 AND is_effect = 1 $res_sql ORDER by is_recommend DESC,is_verify DESC,avg_point DESC,id DESC LIMIT ".app_conf("REC_HOT_LIMIT"));	
				foreach($res_list as $k=>$store){
					//分类
					$cate_list = $GLOBALS['db']->getAll("SELECT dct.* FROM ".DB_PREFIX."deal_cate_type_location_link dctl LEFT JOIN ".DB_PREFIX."deal_cate_type dct ON dct.id =dctl.deal_cate_type_id WHERE dctl.location_id=".$store['id']);
					foreach($cate_list as $kkk=>$vvv){
						$cate_list[$kkk]['url'] = url("store","index",array("cid"=>$store['deal_cate_id'],"tid"=>$vvv['id']));
					}
					$res_list[$k]['cate_list'] = $cate_list;
					//商圈 
					$area_info = $GLOBALS['db']->getRow("SELECT a.id,a.pid,a.name FROM ".DB_PREFIX."area a LEFT JOIN ".DB_PREFIX."supplier_location_area_link slal ON a.id =slal.area_id WHERE slal.location_id=".$store['id']."");
					$res_list[$k]['area'] =  $area_info;
				}
				$GLOBALS['tmpl']->assign("res_list",$res_list);
			}
			$GLOBALS['tmpl']->assign("show_shop_titile",1);
		}
		$GLOBALS['tmpl']->display($template,$cache_id);
	}
	
	public function ajax_hot_store(){
			$hot_where = " deal_type=0 and is_effect=1 and is_hot=1";
			$hot_sortby = " is_recommend desc,sort desc,is_verify desc,dp_count desc ";
			$store_list = get_store_list(5,0,$hot_where,$hot_ortby,false);
			$hot_store_list=$store_list['list'];
			foreach($hot_store_list as $k=>$v)
			{
				$hot_store_list[$k]['avg_point_100']=round($v['avg_point']/5.0,2)*100;
			}				
			$GLOBALS['tmpl']->assign("ajax_store_list",$hot_store_list);
			$html = $GLOBALS['tmpl']->fetch("index_ajax/index_ajax_store.html");
			echo $html;
	}
	
	public function ajax_purpose_store(){
		$purpose_id=$_REQUEST['purpose_id'];
		$city_id = intval($GLOBALS['deal_city']['id']);
		if($city_id>0)
			{			
				$city_ids = load_auto_cache("deal_city_belone_ids",array("city_id"=>$city_id));
			}
		$res_purpose_sub = $GLOBALS['db']->getAll("SELECT pct.* FROM ".DB_PREFIX."purpose_cate_type pct LEFT JOIN ".DB_PREFIX."purpose_cate_type_city_link pctcl ON pctcl.cate_type_id = pct.id WHERE pctcl.recommend=1 AND pctcl.city_id in(".implode(",",$city_ids).") AND pct.cate_id=".$purpose_id." GROUP BY pct.id ORDER BY pctcl.rec_sort DESC LIMIT 5");
		foreach($res_purpose_sub as $kk=>$vv){
			$condition= "";
			$condition = build_deal_filter_condition(array("pid"=>$purpose_id,"sid"=>$vv['id']));		
			$res_purpose_sub[$kk]['count'] = $GLOBALS['db']->getOne("select count(*) from ".DB_PREFIX."supplier_location where deal_type=0 AND is_effect=1 $condition ");
			//推荐的餐厅
			$res_purpose_sub[$kk]['url'] = url("store","index",array("pid"=>$purpose_id,"sid"=>$vv['id']));
		}
		$GLOBALS['tmpl']->assign("res_purpose_sub",$res_purpose_sub);

	
		$ajax_where = " deal_type=0 and is_effect=1 and is_recommend=1";
		$ajax_sortby = " is_recommend desc,sort desc,is_verify desc,dp_count desc ";
		if($purpose_id > 0){
			$unicode_purpose = array();
			$purpose_name= $GLOBALS['db']->getOne("select name from ".DB_PREFIX."purpose_cate where id = ".$purpose_id);
			$append_seo = $append_seo.$purpose_name;
			$unicode_purpose[] = str_to_unicode_string(str_replace("，","",$purpose_name));
			$kw_unicode = implode(" ", $unicode_purpose);
			$ajax_where .= " and (match(purpose_match) against('".$kw_unicode."' IN BOOLEAN MODE))";
		}
		$ajax_store_list = get_store_list(5,0,$ajax_where,$ajax_ortby,false);
		foreach($ajax_store_list['list'] as $k=>$v)
			{
				$ajax_store_list['list'][$k]['avg_point_100']=round($v['avg_point']/5.0,2)*100;
			}	
		$GLOBALS['tmpl']->assign("ajax_store_list",$ajax_store_list['list']);
		
		$GLOBALS['tmpl']->assign("purpose_id",$purpose_id);
		$html = $GLOBALS['tmpl']->fetch("index_ajax/index_ajax_store.html");
		echo $html;
	}
}	
?>